package com.xinlu.user.action;

import java.util.Date;

import com.xinlu.common.exception.LogicException;
import com.xinlu.common.game.bo.BankBackBo;
import com.xinlu.common.game.bo.ConstantLstBo;
import com.xinlu.derek.common.BaseAction;
import com.xinlu.derek.common.BaseForm;
import com.xinlu.persistence.Users;
import com.xinlu.persistence.Wzpz;
import com.xinlu.user.bo.UsersBo;
import com.xinlu.user.dao.MD5Util;
import com.xinlu.user.form.SqtkForm;
import com.xinlu.util.tool.DateUtil;
import com.xinlu.wzpz.bo.WzpzBo;

public class SqtkAction extends BaseAction {

	SqtkForm fr = new SqtkForm();

	private BankBackBo bankBackBo;
	private WzpzBo wzpzBo;
	private UsersBo usersBo;

	public void setUsersBo(UsersBo usersBo) {
		this.usersBo = usersBo;
	}

	public void setWzpzBo(WzpzBo wzpzBo) {
		this.wzpzBo = wzpzBo;
	}

	public void setBankBackBo(BankBackBo bankBackBo) {
		this.bankBackBo = bankBackBo;
	}

	@Override
	public String doExecute() throws Exception {
		return null;
	}

	@Override
	public void formInit() {
	}

	@Override
	public BaseForm getModel() {
		return fr;
	}

	/**
	 * 申请提款
	 * 
	 * @return
	 * @throws Exception
	 */
	public String TiKuan() throws Exception {

		Users crtUser = (Users) getSession().getAttribute("userinfo");
		crtUser = this.usersBo.loadUser(crtUser.getUsername());

		// 增加一条取款记录，并将用户的金额减少
		if (!MD5Util.encodePassword(fr.getTxmm()).toLowerCase().equals(crtUser.getUserpass1().toLowerCase())) {
			throw new LogicException("密码错误.");
		}

		Wzpz wzpz = (Wzpz) getSession().getAttribute("wzpz");
		Date date = new Date();
		int crtH = Integer.parseInt(DateUtil.getFormatDate(DateUtil.HH, date));
		
		Integer qksjks = wzpz.getQksjxzKs();
		Integer qksjjs = wzpz.getQksjxzJs();
		
		if(qksjks>qksjjs){
			boolean rst = false;
			if(crtH<=qksjjs){
				rst = true;
			}else if(crtH>=qksjks){
				rst = true;
			}
			// 结束时间为第二天
			if(!rst){
				throw new LogicException("取款时间不在指定范围，您不能取款.");
			}
		}else{
			// 开始时间与结束时间同一天
			if (!(crtH >= qksjks && crtH <= qksjjs)) {
				throw new LogicException("取款时间不在指定范围，您不能取款.");
			}
		}

		int mtqkzd = wzpz.getMtqkcs();
		if (mtqkzd <= 0) {
			throw new LogicException("系统配置错误，无法提款，请联系管理员!");
		} else {
			int yjqk = bankBackBo.getYjqk(crtUser.getUsername());
			if (yjqk >= mtqkzd) {
				throw new LogicException("您今天不能再提款了，您已经提款" + yjqk + "次了");
			}
			
			Double tqje = ConstantLstBo.getDoubleValue(fr.getTqzj());
			if (tqje < ConstantLstBo.getDoubleValue(wzpz.getZsqkje())) {
				throw new LogicException("每次至少提款" + wzpz.getZsqkje() + "元");
			}
			if (tqje > crtUser.getUermoney()) {
				throw new LogicException("提款金额不能大于" + crtUser.getUermoney() + "元");
			}

			bankBackBo.fqtx(tqje, crtUser);

			crtUser.setUermoney(crtUser.getUermoney() - tqje);
			crtUser.setAllmoney(crtUser.getAllmoney() - tqje);
			usersBo.update(crtUser);

			getSession().setAttribute("userinfo", crtUser);// 更新至会话

			fr.setYe(crtUser.getUermoney());
			fr.setMtqkzd(mtqkzd);
			fr.setSyqk(mtqkzd - yjqk - 1);
			fr.setMczsqx(ConstantLstBo.getDoubleValue(wzpz.getZsqkje()) + "");
			fr.setQksjkss(wzpz.getQksjxzKs() + "");
			fr.setQksjjss(wzpz.getQksjxzJs() + "");

			return loadPage();
		}
	}

	/**
	 * 加载页面
	 * 
	 * @return
	 */
	public String loadPage() {

		Users crtUser = (Users) getSession().getAttribute("userinfo");
		Wzpz wzpz = (Wzpz) getSession().getAttribute("wzpz");

		fr.setYe(crtUser.getUermoney());
		int mtqkzd = wzpz.getMtqkcs();
		if (mtqkzd <= 0) {
			throw new LogicException("系统配置错误，无法提款，请联系管理员!");
		} else {
			int yjqk = bankBackBo.getYjqk(crtUser.getUsername());
			if (yjqk >= mtqkzd) {
//				throw new LogicException("您今天不能再提款了，您已经提款" + yjqk + "次了");
			}
			fr.setMtqkzd(mtqkzd);
			fr.setQksjkss(wzpz.getQksjxzKs() + "");
			fr.setQksjjss(wzpz.getQksjxzJs() + "");
			fr.setSyqk(mtqkzd - yjqk);
			fr.setMczsqx(ConstantLstBo.getDoubleValue(wzpz.getZsqkje()) + "");

			return "sqtk";
		}
	}

}
